﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuanCafe.BusinessLayer;
using QuanCafe.DataLayer;

namespace QuanCafe.admin
{
    public partial class Province : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.fnLoadData();
            }
        }
        /// <summary>
        /// load data
        /// </summary>
        private void fnLoadData()
        {
            try
            {
                QuanCafe.BusinessLayer.Province Province = new QuanCafe.BusinessLayer.Province();
                DataTable dtb = Province.GetAll();
                dtgProvince.DataSource = dtb;
                dtgProvince.DataBind();
                for (int i = 0; i < dtgProvince.Items.Count; i++)
                {
                    if (dtb.Rows[i]["Status"].ToString() == "1")
                    {
                        ((ImageButton)dtgProvince.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnOK.jpg";

                    }
                    else
                    {
                        ((ImageButton)dtgProvince.Items[i].FindControl("btnActive")).ImageUrl = "~/Images/icons/btnKO.jpg";
                    }
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnClearControl()
        {
            txtProvinceID.Text = "";
            txtProvinceName.Text = "";
            txtProvinceCode.Text = "";
            txtNote.Text = "";
            lblError.Text = "";
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            fnClearControl();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                QuanCafe.BusinessLayer.Province ProvinceObj = new QuanCafe.BusinessLayer.Province();
                if (txtProvinceID.Text == "")
                {
                    ProvinceObj.ProvinceName = txtProvinceName.Text.ToString();
                    if (ProvinceObj.CheckProvince() == true)
                    {
                        lblError.Text = "Tên chủ đề này đã tồn tại!Vui lòng nhập lại";
                    }
                    else
                    {
                        ProvinceObj.activity = "Insert";
                        ProvinceObj.ProvinceName = txtProvinceName.Text.Trim();
                        ProvinceObj.ProvinceCode = txtProvinceCode.Text.Trim();
                        ProvinceObj.Note = txtNote.Text.Trim();
                        ProvinceObj.Creator = Session["AdminName"].ToString();
                        ProvinceObj.InsertUpdate();
                        fnLoadData();
                        lblError.Text = "Thêm mới thành công";
                        fnClearControl();
                    }
                }
                else
                {
                    ProvinceObj.activity = "Update";
                    ProvinceObj.ProvinceID = Convert.ToInt32(txtProvinceID.Text.Trim());
                    ProvinceObj.ProvinceName = txtProvinceName.Text.Trim();
                    ProvinceObj.ProvinceCode = txtProvinceCode.Text.Trim();
                    ProvinceObj.Note = txtNote.Text.Trim();
                    ProvinceObj.Creator = Session["AdminName"].ToString();
                    ProvinceObj.InsertUpdate();
                    fnLoadData();
                    lblError.Text = "Cập nhập thành công";
                    fnClearControl();
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string strID = "";
                for (int i = 0; i < dtgProvince.Items.Count; i++)
                {
                    if (((CheckBox)dtgProvince.Items[i].FindControl("chkSelect")).Checked == true)
                    {
                        strID += dtgProvince.Items[i].Cells[0].Text.Trim() + "$";//Cot dau tien phai de cai ID                         
                    }
                }
                CLSCommon.DeleteListRecord("SP_Province", strID, "ProvinceID");
                fnLoadData();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
        public void fnLoadDataByID(int IntID)
        {
            try
            {
                QuanCafe.BusinessLayer.Province Province = new QuanCafe.BusinessLayer.Province();
                Province.ProvinceID = IntID;
                DataRow dr = Province.GetByID();
                txtProvinceID.Text = IntID.ToString();
                txtProvinceName.Text = dr["ProvinceName"].ToString();
                txtProvinceCode.Text = dr["ProvinceCode"].ToString();
                txtNote.Text = dr["Note"].ToString();

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }

        protected void dtgProvince_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string nName = e.CommandName.ToLower();
            int ID = -1;
            switch (nName)
            {
                case "edit":
                    ID = Convert.ToInt32(dtgProvince.Items[e.Item.ItemIndex].Cells[0].Text);
                    fnLoadDataByID(ID);
                    break;
                case "activechange":
                    ID = Convert.ToInt32(dtgProvince.Items[e.Item.ItemIndex].Cells[0].Text);
                    QuanCafe.BusinessLayer.Province Province = new QuanCafe.BusinessLayer.Province();
                    Province.ProvinceID = ID;
                    Province.ChangeActive();
                    break;
            }
            this.fnLoadData();
        }

        protected void dtgProvince_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dtgProvince.CurrentPageIndex = e.NewPageIndex;
            this.fnLoadData();
        }
    }
}
